import pygame
from pygame.sprite import Sprite


class Alien(Sprite):
    def __init__(self,ai_settings,screen):
        super(Alien,self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings

        # 加载图像，设置rect
        self.image = pygame.image.load('../image/alien1.png')
        self.rect = self.image.get_rect()

        # 每个外星人初始位置设置在左上角
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # 出现的准确位置
        self.x = float(self.rect.x)

    # 在指定位置绘制
    def blitme(self):
        self.screen.blit(self.image, self.rect)

    # 边界检测
    def check_edges(self):
        screen_rect = self.screen.get_rect()
        if self.rect.right >= screen_rect.right:
            return True
        elif self.rect.left <= screen_rect.left:
            return True

    # 敌人移动
    def update(self):
        self.x += (self.ai_settings.alien_speed_factor * self.ai_settings.fleet_direction)
        self.rect.x = self.x
